package 左哥算法.compare.comparator;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.function.Consumer;

public class MyComparator {
    int count=1000;
    UseCase useCase;

//    public static void main(String[] args) {
//        Random r=new Random();
//        MyComparator comparator = new MyComparator(10, container -> {
//            container.insert(r.nextInt(10));
//            container.insertln(r.nextInt(10));
//        });
//        comparator.compare(MyComparator::fun,MyComparator::answer);
//    }
//    public static void fun(String[] args){
//        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
//        try {
//            String[] read = in.readLine().split(" ");
//            System.out.println(Integer.parseInt(read[0])*Integer.parseInt(read[1]));
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//    }
//    public static void answer(String[] args){
//        Scanner sc=new Scanner(System.in);
//        int i = sc.nextInt();
//        int b = sc.nextInt();
//        System.out.println(i*b);
//    }

    public MyComparator(UseCase useCase) {
        this.useCase = useCase;
    }

    public MyComparator(int count, UseCase useCase) {
        this.count = count;
        this.useCase = useCase;
    }
    public void compare(Consumer<String[]> solution,Consumer<String[]> answer){
        InputStream originalIn = System.in;
        PrintStream originalOut = System.out;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);
        Throwable throwable=null;
        try {
            // 创建一个ByteArrayOutputStream来存储输出结果
            // 创建一个ByteArrayInputStream来模拟键盘输入
            // 替换System.out和System.in
            System.setOut(printStream);
            while (count-->0){
                Container aCase = useCase.createCase();
                System.setIn(aCase.stream());
                outputStream.reset();
                answer.accept(null);    //标准答案
                String ans = outputStream.toString();
                outputStream.reset();
                System.setIn(aCase.stream());
                String contrast=null;
                try {
                    solution.accept(null);   //比较答案
                    contrast= outputStream.toString();
                }catch (Throwable e){
                    throwable=e;
                    e.printStackTrace();
                }
                if (!ans.equals(contrast)){
                    System.setOut(originalOut);
                    System.setIn(originalIn);
                    System.out.println("---------error-----------");
                    System.out.println(aCase);
                    System.out.println("预期：\n"+ans);
                    System.out.println("实际：\n"+contrast);
                    return;
                }
            }
        }finally {
            // 恢复原始的System.in和System.out
            System.setOut(originalOut);
            System.setIn(originalIn);
        }
        System.out.println("over");
    }
    public interface UseCase{
        default Container createCase(){
            Container container = new Container();
            caseFun(container);
            return container;
        }
        void caseFun(Container container);
    }
}
